//
//  Person.m
//  NSRunTimeDemo
//
//  Created by CPZX010 on 16/9/22.
//  Copyright © 2016年 谢昆鹏. All rights reserved.
//

#import "Person.h"
#import <objc/message.h>


@implementation Person

- (instancetype)init {
    self = [super init];
    if (self) {
        self.name = @"";
        //这个只是交换了方法的实现
        Method eatSomething = class_getInstanceMethod([Person class], @selector(eatSomething));
        Method fucking = class_getInstanceMethod([Person class], @selector(fucking));
        method_exchangeImplementations(eatSomething, fucking);
    }
    return self;
}

+ (void)load {
//    //这个只是交换了方法的实现
//    Method eatSomething = class_getInstanceMethod([Person class], @selector(eatSomething));
//    Method fucking = class_getInstanceMethod([Person class], @selector(fucking));
//    method_exchangeImplementations(eatSomething, fucking);
}


- (void)eatSomething {
    NSLog(@"eating.....");
}

- (void)fucking {
    NSLog(@"fucking.............");
}

+ (void)working {
    NSLog(@"working.........");
}


- (void)printNUM:(NSInteger)num {
    NSLog(@"%ld当前数值为", (long)num);
}



#pragma mark    动态添加方法优势:<一般的方法会一次性的加入到内存当中, 如果调用几率很小则可以使用动态添加方法,节省资源; 懒加载>

void running(id self, SEL sel) {
    NSLog(@"person is Running.....");
    NSLog(@"%@ %@", self, NSStringFromSelector(sel));
}

/**
 *  当一个对象添加了没有实现的方法时会调用此方法处理, 并把对应方法表传递过来
 *
 *  @param sel 想要添加的方法
 *
 *  @return 判断为实现的方法是不是我们想要动态添加的方法
 */
+ (BOOL)resolveInstanceMethod:(SEL)sel {
    if (sel == NSSelectorFromString(@"running")) {
        /**
         *  返回的添加状态
         *
         *  @param self  给哪个类动态添加方法
         *  @param sel  添加方法的方法编号
         *  @param IMP  添加方法的函数实现(函数地址)
         *
         *  @return 函数的类型, (返回值+参数类型) "v":void "@":对象->self  ":":表示SEL->_cmd
         */
    BOOL addStatus = class_addMethod(self, sel, (IMP)running, "v@:");
    return addStatus;
    }
    return [super resolveInstanceMethod:sel];
    
    
    
}








#pragma mark    Case
- (NSMutableDictionary *)paraDic {
    if (!_paraDic) {
        _paraDic = [NSMutableDictionary dictionaryWithCapacity:0];
        for (int i = 0; i < 5; i ++) {
            NSString *name = [NSString stringWithFormat:@"boy%d", i];
            NSString *desc = [NSString stringWithFormat:@"第%d个Boy", i];
            [_paraDic setObject:desc forKey:name];
        }
        [_paraDic setObject:@"testID" forKey:@"id"];
    }
    return _paraDic;
}

- (NSMutableDictionary *)paraRuntimeDic {
    if (!_paraRuntimeDic ) {
        _paraRuntimeDic = [NSMutableDictionary dictionaryWithCapacity:0];
        for (int i = 0; i < 5; i ++) {
            NSString *name = [NSString stringWithFormat:@"girl%d", i];
            NSString *desc = [NSString stringWithFormat:@"第%d个Girl", i];
            [_paraRuntimeDic setObject:desc forKey:name];
        }
        [_paraRuntimeDic setObject:@"TestID" forKey:@"id"];
        [_paraRuntimeDic setObject:@{@"key1" : @"value1", @"key2" : @"value2"} forKey:@"dicKey"];
    }
    return _paraRuntimeDic;
}

















@end
